/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller <philipp.mueller@gmx.de>    *
 *   Copyright (C) 2008 by Tanguy Krotoff <tkrotoff@gmail.com>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtGui>

#include <phonon/audiooutput.h>

#include "widgetplayvideo.h"
#include "qtvomp.h"
#include "vompglobal.h"
#include "enterchannelnumber.h"


WidgetPlayVideo::WidgetPlayVideo( MainWindow* mainWindow, QWidget *parent ) : QWidget( parent ),
    m_mainWindow( mainWindow ),
    m_exit( false ),
    m_switchChannel( false )
{
#ifndef QTVOMP_GSTREAMER
    m_jumpRecording = false;
#endif
}


WidgetPlayVideo::~WidgetPlayVideo()
{
}


void WidgetPlayVideo::initWidgetPlayVideo()
{
    qDebug() << "WidgetPlayVideo::initWidgetPlayVideo called";

    m_exit = false;

    m_mediaObject = new Phonon::MediaObject( this );
    m_mediaObject->setTickInterval( 1000 );

    setupActions();
    setupUi();

    Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput( Phonon::VideoCategory, this );
    Phonon::createPath( m_mediaObject, audioOutput );
    Phonon::createPath( m_mediaObject, m_videoWidget );

    m_volumeSlider->setAudioOutput( audioOutput );

    connect( m_mediaObject, SIGNAL( stateChanged( Phonon::State, Phonon::State ) ),
             this,          SLOT(   stateChanged( Phonon::State, Phonon::State ) ) );
    connect( m_mediaObject, SIGNAL( metaDataChanged() ),
             this,          SLOT(   updateMetaData() ) );

#ifndef QTVOMP_GSTREAMER
    //Playing recordings with Xine, the media object doesn't emit any tick. So do it ourself
    m_tickTimer = new QTimer( this );
    connect( m_tickTimer,   SIGNAL( timeout() ),
             this,          SLOT( emitManualTick() ) );
#endif

    connect( m_mediaObject, SIGNAL( bufferStatus( int ) ),
             this,          SLOT(   bufferStatus( int ) ) );

}

void WidgetPlayVideo::bufferStatus( int percentage )
{
    qDebug()<< "WidgetPlayVideo::bufferStatus: " << percentage;
}

void WidgetPlayVideo::stateChanged( Phonon::State newState, Phonon::State oldState )
{
    QStringList states;
    states << QLatin1String( "Loading" )
           << QLatin1String( "Stopped" )
           << QLatin1String( "Playing" )
           << QLatin1String( "Buffering" )
           << QLatin1String( "Paused" )
           << QLatin1String( "Error" );
    qDebug() << "WidgetPlayVideo::stateChanged: Oldstate: " << states.at( oldState ) << "  NewState: " << states.at( newState );

    switch ( newState )
    {

        case Phonon::ErrorState:
            if ( m_mediaObject->errorType() == Phonon::FatalError )
            {
                QMessageBox::warning( this, tr( "Fatal Error" ), m_mediaObject->errorString() );
            }
            else
            {
                QMessageBox::warning( this, tr( "Error" ), m_mediaObject->errorString() );
            }

            exit();

            return;

        case Phonon::StoppedState:

            if ( !m_exit && m_switchChannel )
            {
                qDebug() << "WidgetPlayVideo::stateChanged: newState = Stop and we change channel";
                switchChannel();
                return;
            }
            else
            {
                if ( m_exit )
                {
                    qDebug() << "WidgetPlayVideo::stateChanged: newState = Stop and we exit";
                    exit();
                    return;
                }
                else
                {
                    qDebug() << "WidgetPlayVideo::stateChanged: New State = stopped";
                    if ( m_ioStream->streamType() == STREAM_TV )
                    {
                        if ( !m_ioStream->streamStarted() )
                        {
                            //We entered stop state, most propabely due to channel not available anymore
                            qDebug() << "WidgetPlayVideo::stateChanged: New State = stopped and was stream started";
                            m_videoWidget->hide();
                            m_channelNotAvailable->show();
                        }
                    }
#ifndef QTVOMP_GSTREAMER
                    if ( m_ioStream->streamType() == STREAM_RECORDING )
                    {
                        if ( m_jumpRecording  )
                        {
                            qDebug() << "WidgetPlayVideo::stateChanged: stopped state, but we want to restart playing";
                            m_jumpRecording = false;
                            m_mediaObject->play();
                        }
                        else
                        {
                            m_recordingSlider->setPos( 0 );
                        }
                    }
#endif
                    m_playAction->setEnabled( true );
                    m_pauseAction->setEnabled( false );
                    m_stopAction->setEnabled( false );
                    return;
                }
            }
            break;

        case Phonon::PlayingState:
            m_playAction->setEnabled( false );
            m_pauseAction->setEnabled( true );
            m_stopAction->setEnabled( true );
            break;

        case Phonon::PausedState:
            m_playAction->setEnabled( true );
            m_pauseAction->setEnabled( false );
            m_stopAction->setEnabled( true );
            break;

        case Phonon::BufferingState:
            if ( m_ioStream->streamType() == STREAM_RECORDING )
            {
                m_playAction->setEnabled( false );
                m_pauseAction->setEnabled( true );
                m_stopAction->setEnabled( true );
            }
            break;

        case Phonon::LoadingState:
            qDebug() << "WidgetPlayVideo::stateChanged: hasVideo" << m_mediaObject->hasVideo();
            break;

        default:
            qDebug() << "WidgetPlayVideo::stateChanged: New State = default/unknown: " << Phonon::PlayingState;
    }
}


void WidgetPlayVideo::updateMetaData()
{
    qDebug() << "WidgetPlayVideo::updateMetaData() called";
    qDebug() << "WidgetPlayVideo::updateMetaData hasVideo" << m_mediaObject->hasVideo();

    QMap<QString, QString> metaData = m_mediaObject->metaData();

    QString title = metaData.value( "TITLE" );

    if ( title == "" )
    {
        title = m_mediaObject->currentSource().fileName();
    }

    m_titelText->setText( title );

    m_artistText->setText( metaData.value( "ARTIST" ) );
    m_albumText->setText( metaData.value( "ALBUM" ) );
    m_yearText->setText( metaData.value( "DATE" ) );

    m_genreText->setText( metaData.value( "GENRE" ) );
    m_trackText->setText( metaData.value( "TRACKNUMBER" ) );
    m_descriptionText->setText( metaData.value( "DESCRIPTION" ) );
}


void WidgetPlayVideo::setupActions()
{
    m_playAction = new QAction( style()->standardIcon( QStyle::SP_MediaPlay ), tr( "Play" ), this );
    m_playAction->setShortcut( tr( "Crl+P" ) );
    m_playAction->setDisabled( true );

    m_pauseAction = new QAction( style()->standardIcon( QStyle::SP_MediaPause ), tr( "Pause" ), this );
    m_pauseAction->setShortcut( tr( "Ctrl+A" ) );
    m_pauseAction->setDisabled( true );

    m_stopAction = new QAction( style()->standardIcon( QStyle::SP_MediaStop ), tr( "Stop" ), this );
    m_stopAction->setShortcut( tr( "Ctrl+S" ) );
    m_stopAction->setDisabled( true );

    m_fullScreen = new QAction( QIcon( ":/images/view-fullscreen.png" ), tr( "Fullscreen" ), this );
    m_fullScreen->setShortcut( tr( "Alt+Enter" ) );
    m_fullScreen->setDisabled( false );

    connect( m_playAction,  SIGNAL( triggered() ), m_mediaObject, SLOT( play() ) );
    connect( m_pauseAction, SIGNAL( triggered() ), m_mediaObject, SLOT( pause() ) );
    connect( m_stopAction,  SIGNAL( triggered() ), m_mediaObject, SLOT( stop() ) );

    connect( m_fullScreen,  SIGNAL( triggered() ), this, SLOT( fullScreenAction() ) );
}


void WidgetPlayVideo::setupUi()
{
    QVBoxLayout *vBox;
    QHBoxLayout *hBox1;
    QHBoxLayout *hBox2;

    vBox = new QVBoxLayout( this );
    hBox1 = new QHBoxLayout();
    hBox2 = new QHBoxLayout();

    m_videoWidget =    new WidgetVideo( this );
    m_osd =            new WidgetOsd( m_mainWindow, m_videoWidget );
    m_metaDataWidget = new QWidget( this );

    //Meta data widget
    m_titelText  = new QLineEdit( "-", this );
    m_titelText->setReadOnly( true );

    m_artistText = new QLineEdit( "-" );
    m_artistText->setReadOnly( true );

    m_albumText = new QLineEdit( "-" );
    m_albumText->setReadOnly( true );

    m_yearText = new QLineEdit( "-" );
    m_yearText->setReadOnly( true );

    m_genreText = new QLineEdit( "-" );
    m_genreText->setReadOnly( true );

    m_trackText = new QLineEdit( "-" );
    m_trackText->setReadOnly( true );

    m_descriptionText = new QLineEdit( "-" );
    m_descriptionText->setReadOnly( true );

    QFormLayout *idLayout = new QFormLayout;
    idLayout->setSpacing( 10 );

    idLayout->addRow( tr( "Title:" ), m_titelText );
    idLayout->addRow( tr( "Artist:" ), m_artistText );
    idLayout->addRow( tr( "Album:" ), m_albumText );
    idLayout->addRow( tr( "Year:" ), m_yearText );
    idLayout->addRow( tr( "Genre:" ), m_genreText );
    idLayout->addRow( tr( "Track ID:" ), m_trackText );
    idLayout->addRow( tr( "Description:" ), m_descriptionText );

    QGridLayout* grid = new QGridLayout();
    grid->addLayout( idLayout, 1, 1 );
    grid->setColumnMinimumWidth( 0, 20 );
    grid->setColumnMinimumWidth( 2, 20 );
    grid->setRowMinimumHeight( 0, 20 );
    grid->setRowMinimumHeight( 2, 20 );

    m_metaDataWidget->setLayout( grid );

    // Toolbar
    m_mediaButtonBar = new QToolBar;

    m_mediaButtonBar->addAction( m_playAction );
    m_mediaButtonBar->addAction( m_pauseAction );
    m_mediaButtonBar->addAction( m_stopAction );

    m_fullScreenBar = new QToolBar;
    m_fullScreenBar->addAction( m_fullScreen );


    QPushButton* backButtonVideo = new QPushButton( tr( "Back" ), this );
    backButtonVideo->setIcon( style()->standardIcon( QStyle::SP_ArrowLeft ) );

    m_recordingSlider = new WidgetRecordingSlider( this );

    m_seekSlider = new Phonon::SeekSlider( this );
    m_seekSlider->setMediaObject( m_mediaObject );
    m_seekSlider->setSingleStep( 10 * 1000 /* 10s */ );
    m_seekSlider->setPageStep( 60 * 1000 /* 1min */ );

    m_volumeSlider = new Phonon::VolumeSlider( this );
    m_volumeSlider->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );

    m_progressBar = new QProgressBar( this );
    m_progressBar->setRange( 0, 100 ); //Anyway default
    m_progressBar->setFixedWidth( 60 );
    m_progressBar->setTextVisible( false );

    //"Channel not available" widget
    m_channelNotAvailable = new QLabel( tr( "Channel not available" ), this );
    m_channelNotAvailable->setAlignment( Qt::AlignCenter );
    m_channelNotAvailable->setAutoFillBackground( true );
    m_channelNotAvailable->setPalette( QPalette( Qt::black ) );
    m_channelNotAvailable->hide();
    vBox->addWidget( m_channelNotAvailable );

    //Videowidget
    hBox1->addWidget( m_videoWidget );
    vBox->addLayout( hBox1 );
    vBox->addWidget( m_metaDataWidget );

    //Control area
    hBox2->addWidget( m_fullScreenBar );
    hBox2->addWidget( m_mediaButtonBar );
    hBox2->addStretch();
    hBox2->addWidget( m_recordingSlider );
    hBox2->addWidget( m_seekSlider );
    hBox2->addWidget( m_volumeSlider );
    hBox2->addWidget( m_progressBar );
    hBox2->addWidget( backButtonVideo );
    vBox->addLayout( hBox2 );

    connect( backButtonVideo,  SIGNAL( clicked() ),
             this,             SLOT( stopPlayVideo() ) );
    connect( this,             SIGNAL( setFullScreen( bool ) ),
             m_videoWidget,    SLOT( setFullScreen( bool ) ) );
    connect( this,             SIGNAL( hideWindowTV( bool ) ),
             m_mainWindow,     SLOT( displayChannelList( bool ) ) );
    connect( this,             SIGNAL( hideWindowRecording( bool ) ),
             m_mainWindow,     SLOT( displayRecordings( bool ) ) );
    connect( this,             SIGNAL( hideWindowMedia( bool ) ),
             m_mainWindow,     SLOT( displayMediaList( bool ) ) );
    connect( m_mediaObject,    SIGNAL( bufferStatus( int ) ),
             m_progressBar,    SLOT( setValue( int ) ) );

    connect( m_recordingSlider, SIGNAL( newPos( quint32 ) ),
             this,              SLOT( recordingSetNewPosition( quint32 ) ) );
}


void WidgetPlayVideo::playTV()
{
    qDebug() << "WidgetPlayVideo::playTV: was called";

    m_videoWidget->setAspectRatio( m_mainWindow->settings()->aspectRatio );
    m_videoWidget->setScaleMode( m_mainWindow->settings()->scaleMode );

    m_exit = false;

    QModelIndexList selectionList = m_mainWindow->channelListSelectionModel()->selectedIndexes();

    if ( selectionList.size() != ChannelListModel::ChannelColumnsLast )
    {
        qWarning() << "playTV: Wrong selection list size: " << selectionList.size();
        return;
    }

    quint32 channel = selectionList[ 0 ].data( ChannelListModel::ChannelNumberRole ).toInt();

    if ( channel == 0 )
    {
        qWarning() << "WidgetPlayVideo::playTV: entry is wrong -> return";
        return;
    }

    m_ioStream = new IOStream( QString(), channel, m_mainWindow->vdr(), STREAM_TV, this );
    m_ioStream->setObjectName( "Channel_" + channel );

    m_seekSlider->hide();
    m_progressBar->show();
    m_progressBar->setValue( 0 );

    m_recordingSlider->hide();
    m_mediaButtonBar->hide();
    m_fullScreenBar->show();
    m_metaDataWidget->hide();

    if( m_ioStream->streamStarted() )
    {
        qDebug() << "WidgetPlayVideo::playTV let's enqueue the stream";
        m_ioStream->waitForReadyRead( 2000 );

        qDebug() << "WidgetPlayVideo::playTV bufferfill done, now set source";

        m_mediaObject->setCurrentSource( m_ioStream );

        qDebug() << "WidgetPlayVideo::playTV source setting done, now lets play";

//        m_ioStream->streamDataReadyRead();
        m_mediaObject->play();

        m_videoWidget->show();
        m_channelNotAvailable->hide();
    }
    else
    {
        qDebug() << "WidgetPlayVideo::playTV stream not started, so show 'Channal unavaiable'";

        m_videoWidget->hide();
        m_channelNotAvailable->show();
    }

    m_mainWindow->centralWidget()->setCurrentIndex( MainWindow::WIDGET_PLAYVIDEO );
    setFocus( Qt::OtherFocusReason );

    m_osd->showOsdTv( selectionList[ 0 ] );
}


void WidgetPlayVideo::playRecording()
{
    qDebug() << "WidgetPlayVideo::playRecording: was called";

    m_videoWidget->setAspectRatio( m_mainWindow->settings()->aspectRatio );
    m_videoWidget->setScaleMode( m_mainWindow->settings()->scaleMode );

    m_exit = false;

    RecordingsWidgetItem* recordingItem = static_cast<RecordingsWidgetItem*>( m_mainWindow->recordingsList()->currentItem() );
    int type = recordingItem->entryType();

    if ( type != REC_TYPE_FILE )
    {
        qDebug() << "WidgetPlayVideo::playRecording: entry is not a file -> return";
        return;
    }

    qDebug() << "WidgetPlayVideo::playRecording: make instance of stream for entry " << recordingItem->entryName()
             << "  with path: " << recordingItem->path();

    m_ioStream = new IOStream( recordingItem->path(), 0, m_mainWindow->vdr(), STREAM_RECORDING, this );
    m_ioStream->setObjectName( recordingItem->path() );

    m_mediaObject->setCurrentSource( m_ioStream );
    m_mediaObject->play();

    m_mediaButtonBar->show();
    m_seekSlider->hide();
    m_recordingSlider->show();
    m_progressBar->show();
    m_progressBar->setValue( 0 );
    m_fullScreenBar->show();
    m_recordingSlider->setPos( 0 );

    m_videoWidget->show();
    m_channelNotAvailable->hide();
    m_metaDataWidget->hide();

    m_mainWindow->centralWidget()->setCurrentIndex( MainWindow::WIDGET_PLAYVIDEO );
    setFocus( Qt::OtherFocusReason );

    m_osd->showOsdRecording( recordingItem );

    //Slider integration
    //TODO: Set recording length: Get the real Frames Per Second and not just assume 25 as we have in Germany normally
    m_recordingSlider->setLength( m_ioStream->recordingFrames() / 25 );

    //Tick doesn't work with xine backend for the recordings, so we need to update the slider differently
#ifdef QTVOMP_GSTREAMER
    connect( m_mediaObject,     SIGNAL( tick( qint64 ) ),
             m_recordingSlider, SLOT( setPos( qint64 ) ) );
#else
    connect( m_tickTimer,       SIGNAL( timeout() ),
             this,              SLOT( emitManualTick() ) );
    connect( this,              SIGNAL( recordingTick( qint64 ) ),
             m_recordingSlider, SLOT( setPos( qint64 ) ) );
    m_tickTimer->start( 10000 ); //Update every 10s the slider position
#endif
}


void WidgetPlayVideo::playMedia()
{
    qDebug() << "WidgetPlayVideo::playMedia: was called";

    m_exit = false;

    MediaWidgetItem* mediaItem = static_cast<MediaWidgetItem*>( m_mainWindow->mediaList()->currentItem() );
    int type = mediaItem->entryType();

    if ( type == MEDIA_TYPE_DIR || type == MEDIA_TYPE_UNKNOWN )
    {
        qDebug() << "MainWindow::playMedia: entry is a directory or type unknown -> return";
        return;
    }

    m_ioStream = new IOStream( mediaItem->path(), 0, m_mainWindow->vdr(), STREAM_MEDIA, this );
    m_ioStream->setObjectName( m_mainWindow->mediaList()->currentItem()->text( 0 ) );

    m_mediaObject->setCurrentSource( m_ioStream );
    m_mediaObject->play();

    m_videoWidget->hide();
    m_channelNotAvailable->hide();
    m_metaDataWidget->show();
    m_mediaButtonBar->show();
    m_fullScreenBar->hide();
    m_seekSlider->show();
    m_recordingSlider->hide();
    m_progressBar->hide();

    m_mainWindow->centralWidget()->setCurrentIndex( MainWindow::WIDGET_PLAYVIDEO );

    setFocus( Qt::OtherFocusReason );
}


void WidgetPlayVideo::exit()
{
    qDebug() << "WidgetPlayVideo::exit called";
    m_mediaObject->clearQueue();
    delete m_ioStream;

    switch ( m_ioStream->streamType() )
    {

        case STREAM_TV:
            emit hideWindowTV( false );
            return;

        case STREAM_RECORDING:
            emit hideWindowRecording( false );
#ifndef QTVOMP_GSTREAMER
            m_tickTimer->stop();
#endif
            return;

        case STREAM_MEDIA:
            emit hideWindowMedia( false );
            return;

        default:
            qWarning() << "WidgetPlayVideo::stateChanged: Unknown streamType!";
            return;
    }
}


void WidgetPlayVideo::switchChannel()
{
    m_mediaObject->clearQueue();
    delete m_ioStream;
    m_switchChannel = false;
    m_videoWidget->show();
    m_channelNotAvailable->hide();
    playTV();
}


void WidgetPlayVideo::stopPlayVideo()
{
    qDebug() << "WidgetPlayVideo::stopPlayVideo: was called";
    m_osd->timer()->stop();
    m_osd->hide();

    if ( m_mediaObject->state() == Phonon::StoppedState )
    {
        exit();
    }
    else
    {
        m_exit = true;
        m_mediaObject->stop();
    }

    qDebug() << "WidgetPlayVideo::stopPlayVideo: has ended";
}


void WidgetPlayVideo::fullScreenAction()
{
    emit setFullScreen( !isFullScreen() );
}


void WidgetPlayVideo::keyPressEvent( QKeyEvent * event )
{
    handleKeyPressEvent( event );
    QWidget::keyPressEvent( event );
}


void WidgetPlayVideo::handleKeyPressEvent( QKeyEvent * event )
{
    switch ( event->key() )
    {

        case Qt::Key_PageUp:
        {
            if ( m_ioStream->streamType() == STREAM_TV )
            {
                QModelIndexList selectionList = m_mainWindow->channelListSelectionModel()->selectedIndexes();

                if ( selectionList.size() != ChannelListModel::ChannelColumnsLast )
                {
                    qWarning() << "WidgetPlayVideo::handleKeyPressEvent: Wrong selection list size: " << selectionList.size();
                    return;
                }

                qDebug() << "WidgetPlayVideo::keyPressEvent: key Up";

                int currentRow = selectionList[ 0 ].row();

                if ( currentRow == m_mainWindow->channelList()->size() - 1 )
                {
                    currentRow = 0;
                }
                else
                {
                    currentRow++;
                }
                m_mainWindow->channelListSelectionModel()->setCurrentIndex( m_mainWindow->channelListModel()->index( currentRow, 0 ),
                                                                            QItemSelectionModel::SelectCurrent |
                                                                            QItemSelectionModel::Rows );

                if( m_mediaObject->state() == Phonon::StoppedState )
                {
                    //We don't have a stream, so "channel is not available" state
                    switchChannel();
                }
                else
                {
                    m_switchChannel = true;

                    m_mediaObject->stop();
                }

                event->accept();

                return;
            }
        }

        case Qt::Key_PageDown:
        {
            if ( m_ioStream->streamType() == STREAM_TV )
            {

                QModelIndexList selectionList = m_mainWindow->channelListSelectionModel()->selectedIndexes();

                if ( selectionList.size() != ChannelListModel::ChannelColumnsLast )
                {
                    qWarning() << "WidgetPlayVideo::handleKeyPressEvent: Wrong selection list size: " << selectionList.size();
                    return;
                }

                qDebug() << "WidgetPlayVideo::keyPressEvent: key Down";

                int currentRow = selectionList[ 0 ].row();

                if ( currentRow == 0 )
                {
                    currentRow = m_mainWindow->channelList()->size() - 1;
                }
                else
                {
                    currentRow--;
                }
                m_mainWindow->channelListSelectionModel()->setCurrentIndex( m_mainWindow->channelListModel()->index( currentRow, 0 ),
                                                                            QItemSelectionModel::SelectCurrent |
                                                                            QItemSelectionModel::Rows );

                if( m_mediaObject->state() == Phonon::StoppedState )
                {
                    //We don't have a stream, so "channel is not available" state
                    switchChannel();
                }
                else
                {
                    m_switchChannel = true;

                    m_mediaObject->stop();
                }

                event->accept();

                return;
            }
        }


        case Qt::Key_Escape:
        {
            if ( m_videoWidget->isFullScreen() )
            {
                qDebug() << "MyVideoWidget::keyPressEvent: Escape";
                emit setFullScreen( false );
                event->accept();
                return;
            }
        }

        case Qt::Key_0:
        case Qt::Key_1:
        case Qt::Key_2:
        case Qt::Key_3:
        case Qt::Key_4:
        case Qt::Key_5:
        case Qt::Key_6:
        case Qt::Key_7:
        case Qt::Key_8:
        case Qt::Key_9:
        {
            if ( m_ioStream->streamType() == STREAM_TV )
            {
                EnterChannelNumber *channel = new EnterChannelNumber( event->text(), m_mainWindow, this );
                bool ok = channel->exec();
                delete channel;

                if ( !ok )
                {
                    event->accept();
                    return;
                }

                if( m_mediaObject->state() == Phonon::StoppedState )
                {
                    //We don't have a stream, so "channel is not available" state
                    switchChannel();
                }
                else
                {
                    m_switchChannel = true;

                    m_mediaObject->stop();
                }

                event->accept();
                return;
            }
        }
    }
}


void WidgetPlayVideo::recordingSetNewPosition( quint32 posTime )
{
#ifndef QTVOMP_GSTREAMER
    m_jumpRecording = true;
#endif
    m_mediaObject->stop();

    //TODO: Set recording length: Get the real Frames Per Second and not just assume 25 as we have in Germany normally
    qint64 dataPos = m_mainWindow->vdr()->positionFromFrameNumber( posTime * 25 );

    m_ioStream->seek( dataPos );
//     m_mediaObject->play(); //Done in state changed
    qDebug() << "WidgetPlayVideo::recordingSetNewPosition called with posTime:" << posTime << "  dataPos:" << dataPos;
}


void WidgetPlayVideo::emitManualTick()
{
    qint64 framePos = m_mainWindow->vdr()->frameNumberFromPosition( m_ioStream->pos() );
    //TODO: Set recording length: Get the real Frames Per Second and not just assume 25 as we have in Germany normally
    emit recordingTick( framePos / 25 * 1000 );
}
